package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    /**
     * 创建床位
     *
     * @param bed
     */
    @Override
    public void createBed(BedDto bed) {
        Bed bed1= BeanUtil.toBean(bed, Bed.class);
        bed1.setCreateTime(LocalDateTime.now());
        bed1.setCreateBy(1L);
        bed1.setBedStatus(0);
        bedMapper.createBed(bed1);
    }

    /**
     * 根据id来查询床位
     *
     * @param id
     * @return
     */
    @Override
    public BedVo getBedById(Long id) {

        return BeanUtil.toBean(bedMapper.getBedById(id),BedVo.class);
    }

    /**
     * 更改床位
     *
     * @param bedDto
     */
    @Override
    public void update(BedDto bedDto) {
       //根据id来查询床位
        BedVo bedVo = getBedById(bedDto.getId());
        //获取当前床位的信息
        bedVo.setSort(bedDto.getSort());
        bedVo.setBedNumber(bedDto.getBedNumber());
        Bed bed = BeanUtil.toBean(bedVo, Bed.class);
        bed.setUpdateTime(LocalDateTime.now());
        bed.setUpdateBy(1L);
        bedMapper.updateBed(bed);
    }

    /**
     * 删除床位
     *
     * @param id
     */
    @Override
    public void deleteBed(Long id) {
        bedMapper.deleteBedById(id);
    }
}

